package com.hl.relearndagger2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hl.relearndagger2.di.componet.DaggerComputorComponent;
import com.hl.relearndagger2.di.module.ComputerFunModule;
import com.hl.relearndagger2.presenter.ComputerPresenter;

import javax.inject.Inject;


public class MainActivity extends AppCompatActivity implements ShowResultView {

    private EditText firstNumber;
    private EditText secondNumber;
    private TextView result;
    private RadioGroup radioGroup;

    @Inject
    public ComputerPresenter computer;

    private static final String[] operators = {
            "#",
            "+",
            "-",
            "*",
            "/",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaggerComputorComponent.builder()
                .computerFunModule(new ComputerFunModule(this))
                .build().inject(this);

        firstNumber = (EditText)findViewById(R.id.first_number);
        secondNumber = (EditText)findViewById(R.id.second_number);
        radioGroup = (RadioGroup) findViewById(R.id.operator);
        result = (TextView) findViewById(R.id.result);
    }

    public void onClick(View view)
    {
        String firstNumber = this.firstNumber.getText().toString();
        String  secondNumber = this.secondNumber.getText().toString();
        String operator = operators[radioGroup.getCheckedRadioButtonId() == -1 ? 0 : radioGroup.getCheckedRadioButtonId()];
        computer.compute(firstNumber,secondNumber,operator);
    }


    @Override
    public void showResult(String result) {
        this.result.setText(result);
    }

    @Override
    public void showErrorPrompt(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
